/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * telaPesquisarCategoria.java
 *
 * Created on 30/11/2012, 20:25:52
 */
package gui;

import Fachada.Fachada;
import classes.Categoria;
import erro.ConexaoException;
import erro.ConsultaException;
import erro.DaoException;
import erro.GeralException;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author aluno
 */
public class telaPesquisarCategoria extends javax.swing.JFrame {

    private static Fachada f;

    /** Creates new form telaPesquisarCategoria */
    public telaPesquisarCategoria() {
        initComponents();
        this.setLocationRelativeTo(null);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSeparator1 = new javax.swing.JSeparator();
        txtBoxPesqNomeCat = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        TableCategoria = new javax.swing.JTable();
        ButtonAltCat = new javax.swing.JButton();
        ButtonCancelar = new javax.swing.JButton();
        jButtonListarTabela = new javax.swing.JButton();
        jButtonPesqCat = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jSeparator2 = new javax.swing.JSeparator();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Nome:");

        TableCategoria.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null},
                {null},
                {null},
                {null}
            },
            new String [] {
                "Nome"
            }
        ));
        TableCategoria.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                TableCategoriaMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(TableCategoria);

        ButtonAltCat.setText("ALTERAR");
        ButtonAltCat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonAltCatActionPerformed(evt);
            }
        });

        ButtonCancelar.setText("CANCELAR");
        ButtonCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonCancelarActionPerformed(evt);
            }
        });

        jButtonListarTabela.setText("LISTAR");
        jButtonListarTabela.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonListarTabelaActionPerformed(evt);
            }
        });

        jButtonPesqCat.setText("PESQUISAR");
        jButtonPesqCat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonPesqCatActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 36));
        jLabel2.setText("Pesquisar Categoria");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(85, 85, 85)
                .addComponent(jLabel2)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(113, 113, 113)
                        .addComponent(ButtonAltCat, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButtonListarTabela, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(ButtonCancelar))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtBoxPesqNomeCat, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButtonPesqCat, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 100, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(22, 22, 22))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(73, Short.MAX_VALUE)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 340, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(71, 71, 71))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(23, 23, 23)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtBoxPesqNomeCat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonPesqCat, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 198, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(23, 23, 23)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ButtonAltCat, javax.swing.GroupLayout.DEFAULT_SIZE, 29, Short.MAX_VALUE)
                    .addComponent(jButtonListarTabela, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ButtonCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void ButtonAltCatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonAltCatActionPerformed

        telaAlterarCategoria alteracat = new telaAlterarCategoria();
        alteracat.setVisible(true);
        Categoria cat = new Categoria();
        f = Fachada.getInstancia();
        cat.setNomeCategoria(txtBoxPesqNomeCat.getText());
        try {
            f.pesquisarCateg(cat);
        } catch (ConexaoException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }catch (GeralException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }catch (DaoException e) {
            JOptionPane.showMessageDialog(null, e.getMessage());
        }
        
        if (cat.getCodCategoria() == null) {
            alteracat.setVisible(false);
            alteracat.dispose();
        } else {
            alteracat.LabelCodCat.setText(Integer.toString(cat.getCodCategoria()));
            alteracat.txtBoxNomeCat.setText(cat.getNomeCategoria());
        }
    }//GEN-LAST:event_ButtonAltCatActionPerformed

    private void TableCategoriaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_TableCategoriaMouseClicked
        int x = TableCategoria.getSelectedRow();
        int y = TableCategoria.getSelectedColumn();

        //Se o clique for duplo
        if (evt.getClickCount() == 1) {
            if (evt.getButton() == evt.BUTTON1) {//E o botão for o esquerdo

                txtBoxPesqNomeCat.setText((String) TableCategoria.getValueAt(x, 0));

            }
        }
    }//GEN-LAST:event_TableCategoriaMouseClicked

    private void ButtonCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonCancelarActionPerformed
        telaPesquisarCategoria tlpesqcat = new telaPesquisarCategoria();
        int i = JOptionPane.showConfirmDialog(null, "Deseja realmente Cancelar?", "Cancelar", JOptionPane.YES_NO_OPTION);
        if (i == JOptionPane.YES_OPTION) {
            telaPesquisarCategoria.this.setVisible(false);
            tlpesqcat.dispose();
        } else {
            repaint();
        }
    }//GEN-LAST:event_ButtonCancelarActionPerformed

    private void jButtonListarTabelaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonListarTabelaActionPerformed
      listarTabela();
      txtBoxPesqNomeCat.setText("");
    }//GEN-LAST:event_jButtonListarTabelaActionPerformed

    private void jButtonPesqCatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonPesqCatActionPerformed
         TableCategoria.getColumnModel().getColumn(0).setPreferredWidth(150);
        DefaultTableModel modelo = (DefaultTableModel) TableCategoria.getModel();
        modelo.setNumRows(0);
        f = Fachada.getInstancia();
        Categoria cat = new Categoria();
        ArrayList<Categoria> lista = new ArrayList<Categoria>();
        try {
            cat.setNomeCategoria(txtBoxPesqNomeCat.getText()+"%");
            lista = (ArrayList<Categoria>) f.pesquisarCat(cat);
            
        } catch (ConexaoException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        } catch (ConsultaException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }
        for (int i = 0; i < lista.size(); i++) {
            modelo.addRow(new Object[]{lista.get(i).getNomeCategoria(),});
        }
        TableCategoria.setModel(modelo);
    }//GEN-LAST:event_jButtonPesqCatActionPerformed

    public void listarTabela() {
        TableCategoria.getColumnModel().getColumn(0).setPreferredWidth(150);
        DefaultTableModel modelo = (DefaultTableModel) TableCategoria.getModel();
        modelo.setNumRows(0);
        f = Fachada.getInstancia();
        Categoria cat = new Categoria();
        ArrayList<Categoria> lista = new ArrayList<Categoria>();
        try {
            lista = (ArrayList<Categoria>) f.listar(cat);
        } catch (ConexaoException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        } catch (ConsultaException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }
        for (int i = 0; i < lista.size(); i++) {
            modelo.addRow(new Object[]{lista.get(i).getNomeCategoria(),});
        }
        TableCategoria.setModel(modelo);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new telaPesquisarCategoria().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton ButtonAltCat;
    private javax.swing.JButton ButtonCancelar;
    private javax.swing.JTable TableCategoria;
    private javax.swing.JButton jButtonListarTabela;
    private javax.swing.JButton jButtonPesqCat;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JTextField txtBoxPesqNomeCat;
    // End of variables declaration//GEN-END:variables
}
